﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using System.Data.SqlClient;

namespace WindowsFormsApplication2.All_FORMS.Lavlah_forms
{
    public partial class OrderCustomer : Form
    {
        SqlConnection conn;
        string id1,id;
        string _id = "101";
        public enum clicktype
        {
            Add = 1,
            Edit = 2,

        }
        clicktype ConsructorType = new clicktype();
        public OrderCustomer(string id, SqlConnection co)
        {
            InitializeComponent();
            id1 = id;
            conn = co;
            ConsructorType = clicktype.Add;
        }
        public OrderCustomer(Order _ord) 
        {
            ConsructorType = clicktype.Edit;
            InitializeComponent();
            _id = _ord.OrderId;
            dateTimePicker1.Text = _ord.OrderAwsanOgnoo;
            dateCYear.Text = _ord.OrderHvrgehOgnoo;
            comboBox2.Text = _ord.OrderStatus;
            textBox8.Text = _ord.ProductId;
            textBox9.Text = _ord.ProductToo;
            textBox6.Text = _ord.XTolologchId;
        }
        private void OrderCustomer_Load(object sender, EventArgs e)
        {
            dataGridViewPro.DataSource = DBTools.selecQryTmp("Select *from Product");
            FormatGrid();
        }
        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewPro.Columns[colname].HeaderText = caption;
            dataGridViewPro.Columns[colname].DisplayIndex = index;
            dataGridViewPro.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        private void FormatGrid()
        {
            dataGridViewPro.AllowUserToOrderColumns = true;
            dataGridViewPro.AllowUserToAddRows = false;
            dataGridViewPro.AllowUserToDeleteRows = false;
            dataGridViewPro.ReadOnly = true;
            dataGridViewPro.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewPro.MultiSelect = true;

            FormatColumn("ProductId", "Бүтээгдэхүүний код", 0);
            FormatColumn("ProductName", "Бүтээгдэхүүний нэр", 1);
            FormatColumn("ProductTypeId", "Бүтээгдэхүүний төрөл", 2);
            FormatColumn("ProductSize", "Бүтээгдэхүүний хэмжээ", 3);
            FormatColumn("ProductPrice", "Нэгж үнэ", 4);
            FormatColumn("ProductToo", "Нийт тоо", 5);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridViewPro_MouseClick(object sender, MouseEventArgs e)
        {
            int i = 0;

            i = dataGridViewPro.CurrentRow.Index;
            DataRowView drv = dataGridViewPro.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string pid = drv[0].ToString();
                string pname = drv[1].ToString();
                string ptype = drv[2].ToString();
                string psize = drv[3].ToString();
                string pprice = drv[4].ToString();
                string ptoo = drv[5].ToString();
                textBox1.Text = pid;
                txtAName.Text = pname;
                textBox2.Text = ptype;
                textBox3.Text = psize;
                textBox4.Text = pprice;
                textBox5.Text = ptoo;

                
            }
        }
        public void FormatColumn1(String colname, String caption, int index)
        {
            dataGridViewOrderCus.Columns[colname].HeaderText = caption;
            dataGridViewOrderCus.Columns[colname].DisplayIndex = index;
            dataGridViewOrderCus.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid1()
        {
            dataGridViewOrderCus.AllowUserToOrderColumns = true;
            dataGridViewOrderCus.AllowUserToAddRows = false;
            dataGridViewOrderCus.AllowUserToDeleteRows = false;
            dataGridViewOrderCus.ReadOnly = true;
            dataGridViewOrderCus.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewOrderCus.MultiSelect = true;

            FormatColumn1("OrderId", "Захиалгын дугаар", 0);
            FormatColumn1("OrderAwsanOgnoo", "Захиалга авсан огноо", 1);
            FormatColumn1("OrderHvrgehOgnoo", "Захиалга хүргэх огноо", 2);
            FormatColumn1("OrderStatusId", "Захиалгын төлөв", 3);
            FormatColumn1("ProductId", "Бүтээгдэхүүний код", 4);
            FormatColumn1("ProductToo", "Тоо хэмжээ", 5);
            FormatColumn1("XTolologchId", "Худалдааны төлөөлөгчийн код", 6);
            FormatColumn1("CustomerId", "Харилцагчийн код", 7);
            FormatColumn1("TolborId", "Төлбөрийн хэлбэр", 8);
        }
        private void btnNextPayment_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            
            SqlCommand comm = new SqlCommand("Select * from  Customers where CustomerId = '" + id1 + "'", conn);
            SqlDataReader reader = comm.ExecuteReader();
            if (reader.HasRows == true)
            {
                reader.Read();
                textBox6.Text = reader["XTolologchId"].ToString();
                txtCName.Text = reader["CustomerFirstName"].ToString();


            }
            else { MessageBox.Show("Алдаа"); }
            
            reader.Close();

            textBox8.Text = textBox1.Text;
            txtpname.Text = txtAName.Text;
            textBox7.Text = textBox4.Text;
            txtCID.Text = id1;
            string aa = string.Format("select *from Orders where {0}='{1}' and {2}=N'{3}'", "CustomerId", txtCID.Text,"OrderStatusId","Идэвхигүй");
            dataGridViewOrderCus.DataSource = DBTools.selecQryTmp(aa);
            FormatGrid1();
            tabPage.SelectedTab = tab2;
        }

 

        private void btnBackSellCar_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true;
            tabPage.SelectedTab = tab1;
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnZahialah_Click(object sender, EventArgs e)
        {
            if (ConsructorType == clicktype.Add)
            {
               Order.AddFunction(_id, dateTimePicker1.Text, dateCYear.Text, comboBox2.Text, textBox8.Text, textBox9.Text, textBox6.Text,txtCID.Text, textBox10.Text);
                MessageBox.Show("Нэмэх үйлдэл");
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}='{3}'", "CustomerId", txtCID.Text ,"XTolologchId", textBox6.Text);
                dataGridViewOrderCus.DataSource = DBTools.selecQryTmp(aa); 
            }
            else
            {

                Order.EditFunction(_id, dateTimePicker1.Text, dateCYear.Text, comboBox2.Text, textBox8.Text, textBox9.Text, textBox6.Text, txtCID.Text, textBox10.Text);
                MessageBox.Show("Засах үйлдэл");
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}='{3}'", "CustomerId", txtCID.Text, "XTolologchId", textBox6.Text);
                dataGridViewOrderCus.DataSource = DBTools.selecQryTmp(aa); 

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Width >= 1210) timer1.Enabled = false;
            else this.Width += 15;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (this.Width <= 1041) timer2.Enabled = false;
            else this.Width -= 15;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;

            i = dataGridViewOrderCus.CurrentRow.Index;
            DataRowView drv = dataGridViewOrderCus.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string proId = drv[0].ToString();

                id = proId;

            }


            string aa11 = string.Format("delete from Orders where OrderId = '{0}'", id);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa11);

            string aa = string.Format("select *from Orders where {0}='{1}' and {2}='{3}'", "CustomerId", txtCID.Text, "XTolologchId", textBox6.Text);
            dataGridViewOrderCus.DataSource = DBTools.selecQryTmp(aa); 
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                string aa = string.Format("select *from Product where {0}='{1}' ", "ProductId", txtFilter.Text);
                dataGridViewPro.DataSource = DBTools.selecQryTmp(aa);
            }
            if (radioButton2.Checked == true)
            {
                string aa = string.Format("select *from Product where {0}='{1}' ", "ProductName", txtFilter.Text);
                dataGridViewPro.DataSource = DBTools.selecQryTmp(aa);
            }
        }
    }
}
